package com.hotel.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotel.entity.ShoppingCart;
import com.hotel.response.Result;
import com.hotel.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Lin
 * @since 2022-06-09
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {


    @Resource
    private ShoppingCartService service;

    @ApiOperation(value = "分页获取购物车信息")
    @GetMapping("/getCartList")
    public Result getCartList(@RequestParam(value = "nickName",required = false) String nickName,
                              @RequestParam(value = "current",required = false,defaultValue = "1")Integer current,
                              @RequestParam(value = "size",required = false,defaultValue = "4")Integer size){
        IPage<ShoppingCart> cartList = service.getCartList(nickName, current, size);
        return Result.ok().data("cartList",cartList);
    }


    @ApiOperation(value = "修改购物车数量")
    @RequiresPermissions({"shopping:update"})
    @PutMapping("/updateCartNumber")
    public Result updateCartNumber(@RequestParam(value = "id") Integer id,
                                   @RequestParam(value = "number")Integer number) {
        int i = service.updateCartNumber(id, number);
        return Result.ok().data("update",i);
    }


    @ApiOperation(value = "根据id删除购物车商品")
    @RequiresPermissions({"shopping:delete"})
    @DeleteMapping("/deleteCart")
    public Result deleteCart(@RequestParam(value = "id") String id) {
        int i = service.deleteCart(id);
        return Result.ok().data("delete",i);
    }

    @ApiOperation(value = "清空用户购物车")
    @RequiresPermissions({"shopping:clear"})
    @DeleteMapping("/clearCart")
    public Result clearCart(@RequestParam(value = "nickName") String nickName) {
        int i = service.clearCart(nickName.trim());
        return Result.ok().data("clear",i);
    }

}

